package cn.stylefeng.guns.modular.menu.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.menu.entity.CanteenUser;
import cn.stylefeng.guns.modular.menu.enums.CanteenUserExceptionEnum;
import cn.stylefeng.guns.modular.menu.mapper.CanteenUserMapper;
import cn.stylefeng.guns.modular.menu.model.param.CanteenUserParam;
import cn.stylefeng.guns.modular.menu.service.CanteenUserService;
import cn.stylefeng.guns.core.enums.CommonStatusEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * canteen_user 服务实现类
 *
 * @author  刘莉
 * @date 2021/06/16 9:18
 */
@Service
public class CanteenUserServiceImpl extends ServiceImpl<CanteenUserMapper, CanteenUser> implements CanteenUserService {

    @Override
    public PageResult<CanteenUser> page(CanteenUserParam canteenUserParam) {

        // 构造条件
        LambdaQueryWrapper<CanteenUser> queryWrapper = new LambdaQueryWrapper<>();

        // 查询未删除的
        queryWrapper.ne(CanteenUser::getStatus, CommonStatusEnum.DELETED.getCode());

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<CanteenUser> list(CanteenUserParam canteenUserParam) {

        // 构造条件
        LambdaQueryWrapper<CanteenUser> queryWrapper = new LambdaQueryWrapper<>();

        // 查询未删除的
        queryWrapper.ne(CanteenUser::getStatus, CommonStatusEnum.DELETED.getCode());

        return this.list(queryWrapper);
    }

    @Override
    public void add(CanteenUserParam canteenUserParam) {

        // 将dto转为实体
        CanteenUser canteenUser = new CanteenUser();
        BeanUtil.copyProperties(canteenUserParam, canteenUser);

        // 设置状态为启用
        canteenUser.setStatus(CommonStatusEnum.ENABLE.getCode());

        this.save(canteenUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CanteenUserParam canteenUserParam) {

        // 根据id查询实体
        CanteenUser canteenUser = this.queryCanteenUser(canteenUserParam);

        // 逻辑删除，修改状态
        canteenUser.setStatus(CommonStatusEnum.DELETED.getCode());

        // 更新实体
        this.updateById(canteenUser);

    }

    @Override
    public void edit(CanteenUserParam canteenUserParam) {

        // 根据id查询实体
        CanteenUser canteenUser = this.queryCanteenUser(canteenUserParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(canteenUserParam, canteenUser);

        // 不能修改状态，用修改状态接口修改状态
        canteenUser.setStatus(null);

        this.updateById(canteenUser);
    }

    @Override
    public CanteenUser detail(CanteenUserParam canteenUserParam) {
        return this.queryCanteenUser(canteenUserParam);
    }

    @Override
    public void changeStatus(CanteenUserParam canteenUserParam) {
        Long id = canteenUserParam.getId();
        Integer status = canteenUserParam.getStatus();

        // 校验状态在不在枚举值里
        CommonStatusEnum.validateStatus(status);

        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<CanteenUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
            .eq(CanteenUser::getId, id)
            .and(i -> i.ne(CanteenUser::getStatus, CommonStatusEnum.DELETED.getCode()))
            .set(CanteenUser::getStatus, status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }


    /**
     * 获取canteen_user
     *
     * @author  刘莉
     * @since 2021/06/16 9:18
     */
    private CanteenUser queryCanteenUser(CanteenUserParam canteenUserParam) {
        CanteenUser canteenUser = this.getById(canteenUserParam.getId());
        if (ObjectUtil.isEmpty(canteenUser)) {
            throw new ServiceException(CanteenUserExceptionEnum.NOT_EXISTED);
        }
        return canteenUser;
    }
}